package com.example.controller;

import com.example.domian.Dept;
import com.example.domian.DeptEmp;
import com.example.result.Result;
import com.example.service.DeptService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

    //查询所有部门
    @GetMapping("/selectAll")
    public Result selectAll(){
        List<Dept> selectAll = deptService.selectAll();
        return Result.success(selectAll);
    }

    //根据id查询部门
    @GetMapping("/selectById")
    public Result selectById(@RequestParam("id") Integer id){
        Dept dept = deptService.selectById(id);
        return Result.success(dept);
    }

    @DeleteMapping("/deleteById")
    //根据id删除部门
    public Result deleteById(@RequestParam("id") Integer id){
        deptService.deleteById(id);
        return Result.success("删除成功");
    }

    //添加部门
    @PostMapping("/add")
    public Result add(@RequestParam("name") String name){
        deptService.add(name);
        return Result.success("添加成功");
    }

    //修改部门
    @PostMapping("/update")
    public Result update(@RequestParam("id") Integer id,@RequestParam("name") String name){
        deptService.update(id,name);
        return Result.success("修改成功");
    }

    //根据id查询部门包含员工
    @GetMapping("/withEmp/{id}")
    public Result findByIdWithEmp(@PathVariable Integer id) {
        //调用业务层接口方法根据id查询部门,包含部门所属的员工
        DeptEmp DeptEmp = deptService.findByIdWithEmp(id);
        if (DeptEmp == null) {
            return Result.success("您查询的部门不存在!!!");
        }
        return Result.success(DeptEmp);
    }
}
